package com.swak.reactivex.cluster.funcs;

import java.util.List;
import java.util.Set;

import com.swak.reactivex.cluster.ClusterExchangeHandler;
import com.swak.reactivex.cluster.Member;
import com.swak.reactivex.transport.funcs.ExchangeHandlers;
import com.swak.utils.Sets;

public class ClusterExchangeHandlers extends ExchangeHandlers implements ClusterExchangeHandler {

	protected Set<ClusterMemberShipChanagedHandler> memberShipChanagedHandlers = null;
	
	public void addClusterMemberShipChanagedHandler(ClusterMemberShipChanagedHandler handler) {
		if (memberShipChanagedHandlers == null) {
			synchronized (this) {
				if (memberShipChanagedHandlers == null) {
					memberShipChanagedHandlers = Sets.newConcurrentSet();
				}
			}
		}
		memberShipChanagedHandlers.add(handler);
	}

	public void removeClusterMemberShipChanagedHandler(ClusterMemberShipChanagedHandler handler) {
		if (memberShipChanagedHandlers == null) {
			return;
		}
		memberShipChanagedHandlers.remove(handler);
	}
	
	@Override
	public void memberShipChanaged(Member member, List<Member> members, boolean add) {
		if (memberShipChanagedHandlers == null) {
			return;
		}
		for (ClusterMemberShipChanagedHandler handler : memberShipChanagedHandlers) {
			handler.memberShipChanaged(member, members, add);
		}
	}
}
